package com.cxb.view;

import java.util.HashMap;
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cxb.R;

public class CompassView2 extends LinearLayout {

	int width;
	int height;
	private ImageView iv_compass;
	private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
	boolean isShowCompass = false;
	boolean isDrawCompass = false;
	private View compass;

	public CompassView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.compass, this);
		compass = findViewById(R.id.compass);
		iv_compass = (ImageView) findViewById(R.id.iv_compass);
		this.setBackgroundResource(R.drawable.compass_bg);
		iv_compass.setImageResource(R.drawable.n);

		map.put(0, R.drawable.n);
		map.put(180, R.drawable.s);
		map.put(270, R.drawable.e);
		map.put(90, R.drawable.w);
		map.put(135, R.drawable.ws);
		map.put(225, R.drawable.es);
		map.put(45, R.drawable.wn);
		map.put(315, R.drawable.en);
		// map.put(180, R.drawable.s);
		// map.put(270, R.drawable.w);
		// map.put(90, R.drawable.e);
		// map.put(135, R.drawable.es);
		// map.put(225, R.drawable.ws);
		// map.put(45, R.drawable.en);
		// map.put(315, R.drawable.wn);

	}

	public void setShowCompass(boolean isShow) {
		isShowCompass = isShow;
		if (isShow) {
			// if (degress >= 0) {
			// this.setBackgroundResource(R.drawable.compass_bg);
			// } else {
			this.setBackgroundResource(R.drawable.compass_bg);
			compass.setVisibility(View.VISIBLE);
			// }
		} else {
			compass.setVisibility(View.GONE);
			this.setBackgroundResource(R.drawable.setting_up_map);
		}
		invalidate();
	}

	public Animation getRoatationAnimation(final int nowAngle, final int toAngle) {

		final RotateAnimation animation = new RotateAnimation(0f, 45f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animation.setDuration(200);// 设置动画持续时间
		// animation.setRepeatCount(RotateAnimation.INFINITE);
		// animation.setFillEnabled(true);
		// animation.setFillAfter(true);
		// animation.setFillAfter(true);
		animation.setInterpolator(new LinearInterpolator());
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				int currentAngle = nowAngle + 45;
				Log.i("test", "currentAngle =" + currentAngle + " toAngle="
						+ toAngle);
				iv_compass.setImageResource(map.get(currentAngle % 360));

				int compareAngle = toAngle == 0 ? 360 : toAngle;
				if (currentAngle < compareAngle) {
					iv_compass.startAnimation(getRoatationAnimation(
							currentAngle, toAngle));
				}
			}

		});
		return animation;
	}

	int degress = 0;

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		width = this.getWidth();
		height = this.getHeight();
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	int preDegree = 0;

	public void rotateCompass(float angle) {

		Log.i("test", "angle=" + angle);
		// if (angle == -1) {
		// this.setBackgroundResource(R.drawable.gps_locationing);
		//
		// compass.setVisibility(View.GONE);
		// return;
		// } else
		if (angle==-1||angle == -2) {
			this.setBackgroundResource(R.drawable.compass_bg);
			compass.setVisibility(View.VISIBLE);
			return;
			// angle = 0;
		}

		int rate = (int) Math.round(angle / 45.0);
		int actualdegress = (Math.round(rate) * 45) % 360;
		Log.i("test", "1 degress=" + actualdegress + " preDegree=" + preDegree);

		if (preDegree != actualdegress) {
			startRotateAnimation(degress, (360 - actualdegress) % 360);

			Log.i("test", "2 rate=" + rate + " actualdegress=" + actualdegress
					+ " preDegree=" + preDegree);
			preDegree = actualdegress;

			invalidate();
		}

	}

	public void startRotateAnimation(int preDegress, int nowDegree) {

		Log.i("test", "preDegress=" + preDegress + " nowDegree=" + nowDegree);

		iv_compass.startAnimation(

		getRoatationAnimation(preDegress, nowDegree % 360));

	}

	// public Animation getRoatationAnimation(int preDegress, final int to) {
	// Log.i("test", "to=" + to);
	// final RotateAnimation animation = new RotateAnimation(0f, Math.abs(to
	// - preDegress), Animation.RELATIVE_TO_SELF, 0.5f,
	// Animation.RELATIVE_TO_SELF, 0.5f);
	// animation.setDuration(Math.abs(preDegress - to) / 45 * 500);//
	// ���ö�������ʱ��
	// // animation.setRepeatCount(RotateAnimation.INFINITE);
	// animation.setFillEnabled(true);
	// // animation.setFillAfter(true);
	// // animation.setFillAfter(true);
	// animation.setInterpolator(new LinearInterpolator());
	// animation.setAnimationListener(new AnimationListener() {
	//
	// @Override
	// public void onAnimationStart(Animation animation) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void onAnimationRepeat(Animation animation) {
	//
	// }
	//
	// @Override
	// public void onAnimationEnd(Animation animation) {
	// iv_compass.setImageResource(map.get(to));
	//
	// }
	//
	// });
	// return animation;
	// }
	boolean first = true;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
//		if (event.getAction() == MotionEvent.ACTION_DOWN) {
//
//			rotateCompass(new Random().nextInt(360));
//
//		}
		return super.onTouchEvent(event);
	}

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);
	}
}
